<?php
/**
 * This file is part of HelloWorld.
 *
 */

namespace common\services;

use common\components\WechatMini;
use common\exmodels\WxQrCodeLogModel;
use common\services\qrcode\WxQrCodeLogService;
use Yii;
use common\helpers\CommonHelper;
use yii\db\Exception;

class ActivityService
{
    /**
     * 获取小程序二维码
     * @return array
     */
    public function poster($params)
    {
        $qrCode = WxQrCodeLogModel::find()->where(['member_id' => $params['member_id']])->limit(1)->one();
        if (!empty($qrCode->member_id)) {
            return CommonHelper::toResult(0, '', ['url' => $qrCode->url]);
        }
        try {
            $params['gid'] = Yii::$app->gid->getId();
            $wechat = new WechatMini();
            $res = $wechat->getWxACode($params);
            if (CommonHelper::isJson($res)) {
                return CommonHelper::toResult(-3, '获取小程序码失败');
            }
            $result = Yii::$app->aliyunOss->putObject('wxcode', $res, null, false, 'png');
            if ($result['code']) {
                return CommonHelper::toResult($result['code'] ?? -1, $result['msg'] ?? '获取失败');
            }
            #图片加图片水印
            $img = Yii::$app->params['activityUrlImg'];
            $url = Yii::$app->aliyunOss->getImgWithWaterMark($img, $result['data']['object'], 4070880000 - time(), 28)['data']['url'];
            $memberExtend = MemberExtendService::getExtend(['member_id' => $params['member_id']]);
            $data = [
                'id' => (string)$params['gid'],
                'member_id' => $params['member_id'],
                'activity_code' => $params['activity_code'],
                'invite_code' => $memberExtend->invite_code ?? '',
                'realname' => Yii::$app->member->getIdentity()->realname ?? '',
                'channel' => $params['channel'],
                'url' => $url ?? '',
            ];
            $model = new WxQrCodeLogService();
            $createRes = $model->create($data);
            if ($createRes['code'] != 0) {
                throw new Exception($createRes['msg'] ?? '创建微信二维码记录失败');
            }
            return CommonHelper::toResult(0, '', ['url' => $url]);
        } catch (\Exception $e) {
            Yii::error($e);
            return CommonHelper::toResult(-99, '获取分享码失败');
        }
    }

    /**
     * @param $params
     * @return array
     */
    public function inviteInfo($params)
    {
        $info = WxQrCodeLogService::find()->where(['id' => $params['id']])->limit(1)->one();
        if (empty($info->id)) {
            return CommonHelper::toResult(-1, '邀请信息不存在');
        }
        return CommonHelper::toResult(0, '', $info);
    }

    /**
     * 处理数据.
     * @param $data
     * @return mixed
     */
    public static function handleData($data)
    {
        if (! $data) {
            return $data;
        }

        if (array_key_exists('activity_status', $data)) {
            $data['activity_status_text'] = Yii::$app->params['activityStatus'][$data['activity_status']] ?? $data['activity_status'];
        }
        if (array_key_exists('activity_type', $data)) {
            $data['activity_type_text'] = Yii::$app->params['activityType'][$data['activity_type']] ?? $data['activity_type'];
        }

        return $data;
    }

    /**
     * 处理列表数据.
     */
    public static function handleList(array $data)
    {
        if (! $data) {
            return $data;
        }

        foreach ($data as $k => $v) {
            $data[$k] = static::handleData($v);
        }

        return $data;
    }
}